﻿using System;
using Epitech.Krisk.Kinect.Sensor;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Epitech.Krisk.Xna.ScreenManagerLib.Widget;
using Epitech.Krisk.Xna.UI.Game.Utils;
using Microsoft.Xna.Framework;

namespace Epitech.Krisk.Xna.UI.Game.Menu
{
    class MainMenu : MenuScreen
    {
        RenderBackground _bg;

        public MainMenu()
            : base("")
        {
            // Create our menu entries.
            LayoutEntry menu = new LayoutEntry(new Vector2(1575f, 485f))
                                   {Alignement = LayoutEntry.TextAlignement.Right, LineSpacing = 70};

            MenuEntry playGameMenuEntry = new MenuEntry("[ Nouvelle partie ]");
            MenuEntry loadMenuEntry = new MenuEntry("[ Charger ]");
            MenuEntry optionsMenuEntry = new MenuEntry("[ Options ]");
            MenuEntry creditsMenuEntry = new MenuEntry("[ Credits ]");
            MenuEntry exitMenuEntry = new MenuEntry("[ Quitter ]");

            playGameMenuEntry.Selected += DisplayGameOptions;
            loadMenuEntry.Selected += DisplayLoad;
            optionsMenuEntry.Selected += DisplayOptions;
            creditsMenuEntry.Selected += DisplayCredits;
            exitMenuEntry.Selected += OnCancel;

            menu.AddEntry(playGameMenuEntry);
            menu.AddEntry(loadMenuEntry);
            menu.AddEntry(optionsMenuEntry);
            menu.AddEntry(creditsMenuEntry);
            menu.AddEntry(exitMenuEntry);
            // Add entries to the menu.
            MenuEntries.Add(menu);

            foreach (MenuEntry item in menu.MenuEntries)
            {
                item.FontSize = 1f;
                item.SelectedColor = new Color(119, 121, 81);
                item.DefaultColor = new Color(208, 44, 6);
                item.IsPulse = false;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            _bg.Draw(gameTime);
            base.Draw(gameTime);
        }

        public override void LoadContent()
        {
            _bg = new RenderBackground("Game/MenuBackground");
            _bg.Content = ScreenManager.Game.Content;
            _bg.SpriteBatch = ScreenManager.SpriteBatch;
            _bg.ViewPort = ScreenManager.GraphicsDevice.Viewport;
            _bg.LoadContent();
            base.LoadContent();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            _bg.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        #region Handle Input


        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.Game.Exit();
        }

        protected void DisplayGameOptions(object sender, PlayerIndexEventArgs playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
            ScreenManager.AddScreen(new GameOptionsView(), null);
        }

        protected void DisplayLoad(object sender, PlayerIndexEventArgs playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
            ScreenManager.AddScreen(new LoadView(), null);
        }

        protected void DisplayOptions(object sender, PlayerIndexEventArgs playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
            ScreenManager.AddScreen(new OptionsView(), null);
        }

        protected void DisplayCredits(object sender, PlayerIndexEventArgs playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
            ScreenManager.AddScreen(new CreditsView(), null);
        }

        #endregion
    }
}
